package guestbook;
import javax.servlet.http.*;

/*
 * this class will run based on the cron jobs. it fetches the totoForm information
 * and stores it in the DB
 */
public class gettingTotoForm extends HttpServlet 
{    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	 {
		try 
		{
			String url="http://www.dapper.net/RunDapp?dappName=totoForm_totoSite_221209&v=1&applyToUrl=http://www.toto.org.il/85_normal.asp";
			String field=null;
			String[] result=null;
			String[] days= new String[16];
			String[] times= new String[16];
			String[] leagues= new String[16];
			String[] teams= new String[16];
			boolean flag=true;
		
			totoForm form=new totoForm(days,times,leagues,teams);
					
			/* this loop gets the data for all relevant fields*/
			for (int i=1;i<5;i++){
				switch (i) {
					case 1: field="match_day";break;
					case 2: field="match_time";break;
					case 3: field="league";break;
					case 4: field="teams"; break;
				}
				result=parsingSites.getDataFromXml(result, field, url);
			
				/* if the result is null, it means a dapper approach failed*/
				if (result==null){
					resp.getWriter().println("error");
					flag=false;
					return;
				}			
			
				switch (i) {
					case 1: form.setDay(result);break;
					case 2: form.setTime(result);break;
					case 3: form.setLeague(result);break;
					case 4: form.setTeams(result); break;
				}
			
			/* when the code above is complete, we have a form object ready for DB*/
			}			
				
		  /* the code below stores the form object in the DB, for quicker approach
		  if the flag is true, it means result was never null*/
			DBMethods.saveInDB(form,flag);
			
			/* returns success to caller if all is well*/
			resp.setContentType("text/plain;charset=UTF-8");
			resp.getWriter().println("success");
		
		} catch (Exception e)
		{
			e.printStackTrace();
		};		
	}
			
}
